package com.yidu.ladder.base.controller;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yidu.ladder.base.domain.Riparea;
import com.yidu.ladder.base.domain.RipareaCustomer;
import com.yidu.ladder.base.domain.RipareaStaff;
import com.yidu.ladder.base.domain.RipareaVo;
import com.yidu.ladder.base.domain.Staff;
import com.yidu.ladder.base.service.RipareaService;
import com.yidu.ladder.system.domain.Customer;
/**
 * 定区控制器
 * @author lw
 * @since 2020/1/10
 */
@Controller
@RequestMapping("/basic")
public class RipareaController {
	
	/**
	 * 将定区业务层依赖注入
	 */
	@Autowired
	private RipareaService ripareaService;

	public RipareaController() {
		super();
	}

	public RipareaController(RipareaService ripareaService) {
		super();
		this.ripareaService = ripareaService;
	}

	public RipareaService getRipareaService() {
		return ripareaService;
	}

	public void setRipareaService(RipareaService ripareaService) {
		this.ripareaService = ripareaService;
	}
	
	/**
	 * 组合查询所有带分页
	 * @param ripareaVo
	 * @return
	 */
	@GetMapping("/ripareas")
	@ResponseBody
	public Map<String,Object> findAllRiparea(RipareaVo ripareaVo){
		Map<String, Object> result = ripareaService.findAllRipareaWithPage(ripareaVo);
		return result;
	}
	
	/**
	 * 添加定区
	 * @param riparea
	 * @return
	 */
	@PostMapping("/riparea")
	@ResponseBody
	public boolean saveRiparea(Riparea riparea) {
		boolean result = ripareaService.addRiparea(riparea);
		return result;
	}
	
	/**
	 * 修改定区
	 * @param riparea
	 * @return
	 */
	@PutMapping("/riparea")
	@ResponseBody
	public boolean updateRiparea(Riparea riparea) {
		boolean result = ripareaService.updataRiparea(riparea);
		return result;
	}
	
	/**
	 * 逻辑删除
	 * @param ripAreaIdStr
	 * @return
	 */
	@PostMapping("/frozenORrecoverRiparea")
	@ResponseBody
	public boolean frozenORrecoverRiparea(String ripAreaIdStr) {
		boolean result = ripareaService.frozenORrecoverRiparea(ripAreaIdStr, true);
		return result;
	}
	
	/**
	 * 还原
	 * @param ripAreaIdStr
	 * @return
	 */
	@PostMapping("/restoreRrecoverRiparea")
	@ResponseBody
	public boolean restoreRrecoverRiparea(String ripAreaIdStr) {
		boolean result = ripareaService.frozenORrecoverRiparea(ripAreaIdStr, false);
		return result;
	}
	
	/**
	 * 根据定区Id查询职员
	 * @param ripAreaId
	 * @return
	 */
	@GetMapping("/staffs/{ripAreaId}")
	@ResponseBody
	public List<Staff> findAllStaffByRipAreaId(@PathVariable String ripAreaId){
		List<Staff> result = ripareaService.findAllStaffByRipAreaId(ripAreaId);
		return result;
	}
	
	/**
	 * 根据定区Id查询职员
	 * @param ripAreaId
	 * @return
	 */
	@GetMapping("/RipareaStaffs/{ripAreaId}")
	@ResponseBody
	public List<RipareaStaff> findRipareaStaffByRipAreaId(@PathVariable String ripAreaId){
		List<RipareaStaff> result = ripareaService.findRipareaStaffByRipAreaId(ripAreaId);
		return result;
	}
	
	/**
	 * 添加定区职员和删除定区职员
	 * @param ripAreaId
	 * @param str
	 * @param index
	 * @return
	 */
	@PostMapping("/ripareaStaff/{ripAreaId}/{str}/{index}")
	@ResponseBody
	public boolean saveRipareaStaff(@PathVariable String ripAreaId,@PathVariable String str,@PathVariable Integer index) {
		//把Id用逗号隔开
		String[] rstIds = str.split(",");
		//调用方法查询定区名
		Riparea riparea = ripareaService.findRipareaByRipareaId(ripAreaId);
		//定义一个布尔型的变量为false
		boolean result = false;
		//判断从前台接收的index是否等于0
		if (index.equals(0)) {
			//遍历职员Id集合
			for (int i = 0; i < rstIds.length; i++) {
				//调用方法查询职员名
				Staff staff = ripareaService.findStaffByStaffId(rstIds[i]);
				//添加定区职员
				result = ripareaService.addRipareaStaff(riparea, staff);
			}
		} else {
			//遍历职员Id集合
			for (int j = 0; j < rstIds.length; j++) {
				//调用删除方法删除定区职员
				result = ripareaService.deleteRipareaStaff(ripAreaId, rstIds[j]);
			}
		}
		return result;
	}
	
	/**
	 * 查询所有客户Id和客户名
	 * @return
	 */
	@GetMapping("/customers")
	@ResponseBody
	public List<Customer> findAllCustomer(){
		List<Customer> result = ripareaService.findAllCustomer();
		return result;
	}
	
	/**
	 * 根据定区Id查询客户信息
	 * @param ripAreaId
	 * @return
	 */
	@GetMapping("/customers/{ripAreaId}")
	@ResponseBody
	public List<RipareaCustomer> findRipareaCustomerByRipAreaId(@PathVariable String ripAreaId){
		List<RipareaCustomer> result = ripareaService.findRipareaCustomerByRipAreaId(ripAreaId);
		return result;
	}
	
	/**
	 * 添加定区客户和删除定区客户
	 * @param ripAreaId
	 * @param str
	 * @param index
	 * @return
	 */
	@PostMapping("/ripareaCustomer/{ripAreaId}/{str}/{index}")
	@ResponseBody
	public boolean saveRipareaCustomer(@PathVariable String ripAreaId,@PathVariable String str,@PathVariable Integer index) {
		//把Id用逗号隔开
		String[] customerIds = str.split(",");
		//调用方法查询定区名
		Riparea riparea = ripareaService.findRipareaByRipareaId(ripAreaId);
		//定义一个布尔型的变量为false
		boolean result = false;
		//判断从前台接收的index是否等于0
		if(index.equals(0)) {
			//遍历客户Id集合
			for (int i = 0; i < customerIds.length; i++) {
				//调用方法查询客户名
				Customer customer = ripareaService.findCustomerByCustomerId(customerIds[i]);
				//添加定区客户
				result = ripareaService.addRipareaCustomer(riparea, customer);
			}
		}else {
			//遍历客户Id集合
			for (int i = 0; i < customerIds.length; i++) {
				//调用删除方法删除定区客户
				result = ripareaService.deleteRipareaCustomer(ripAreaId, customerIds[i]);
			}
		}
		return result;
	}
	
	/**
	 * 查询所有的职员名
	 * @return
	 */
	@GetMapping("/staffName")
	@ResponseBody
	public List<Staff> findAllStaffName(){
		List<Staff> result = ripareaService.findAllStaffName();
		return result;
	}
	
}
